// pages/finance/withdraw/withdraw/withdraw.js
var http = require('../../../../utils/http.js');
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    isShowChooseBankcard: false,
    isActive: false,
    activeIndex: 0,
    isOpen: true,//是否开启短信通知
    moneyObj: {},//显示待入帐金额和可用余额
    userCardList: [],
    withdrawForm: {
      cardholder: '',//持卡人姓名
      cardNumber: '',//分割后的银行卡号
      card_id: '',//银行卡关联id
      order_money: '',//提现金额
      order_remarks: '',//提现标注
      order_fees: 1,//提现扣除手续费
    }
  },
  hideChooseBankcard(){
    this.setData({
      "isShowChooseBankcard": false
    });
  },
  showChooseBankcard(){
    this.setData({
      "isShowChooseBankcard": true
    });
  },
  //选择银行卡
  chooseBankCard(e){
    this.setData({
      "activeIndex": e.detail
    });
  },
  //获取银行卡信息接口
  getUserCardList() {
    let that = this;
    http.GET({
      url: 'api/user/userCardInfo',
      success: function (res) {
        if (res.data.error === 0) {
          let list = res.data.data;
          for(let index=0;index<list.length;index++){
            if(index===0){
              list[index].isActive = true;
            } else {
              list[index].isActive = false;
            }
          }
          that.setData({
            "userCardList": list
          });
        }
      }
    })
  },
  //去提现步骤3
  toStep3() {
    let that = this;
    if (parseFloat(this.data.withdrawForm.order_money) > parseFloat(this.data.moneyObj.userMoney)) {
      wx.showToast({
        title: '转账金额不能大于可用余额',
        icon: 'none'
      })
      return false;
    }
    this.setData({
      "withdrawForm.card_id": this.data.userCardList[this.data.activeIndex].id
    });
    this.setData({
      "withdrawForm.cardholder": this.data.userCardList[this.data.activeIndex].cardholder
    });
    this.setData({
      "withdrawForm.card_number": this.data.userCardList[this.data.activeIndex].card_number
    });
    this.setData({
      "withdrawForm.cardNumber": this.data.userCardList[this.data.activeIndex].card_number.replace(/[\s]/g, '').replace(/(\d{4})(?=\d)/g, "$1 ")
    });
    this.setData({
      "withdrawForm.bankname": this.data.userCardList[this.data.activeIndex].bankname
    });
    wx.setStorage({
      key: 'withdrawForm',
      data: that.data.withdrawForm
    })
    wx.navigateTo({
      url: '../step3/step3',
    });
    
  },
  //检测输入转账金额
  watchInputMoney(event){
    let money = event.detail.value;
    if(isNaN(money)){
      this.setData({
        "isActive": true
      });
      return 10;
    } else {
      if(money>=10){
        if (money >= 10 && money < 1000){
          this.setData({
            "withdrawForm.order_fees": 1
          });
        } else if (money >= 1000 && money <= 25000) {
          this.setData({
            "withdrawForm.order_fees": money * 1.0 / 1000
          });
        } else {
          this.setData({
            "withdrawForm.order_fees": 25
          });
        }
        this.setData({
          "withdrawForm.order_money": parseInt(money)
        });
        this.setData({
          "isActive": true
        });
      } else {
        this.setData({
          "isActive": false
        });
      }
      
    }
  },
  //检测输入提现标注
  watchInputMark(e){
    this.setData({
      "withdrawForm.order_remarks": e.detail.value
    })
  },
  //是否开启短信通知收款方
  isSendMessage(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否确定开启短信通知收款方？关闭后受收款方将无法收到通知',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            "isOpen": true
          });
        } else if (res.cancel) {
          that.setData({
            "isOpen": false
          });
        }
      }
    })
  },
  //跳转到收费标准页面
  toCharge(){
    wx.navigateTo({
      url: '../charge/charge',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserCardList();
    let that = this;
    wx.getStorage({
      key: 'moneyObj',
      success: function (res) {
        that.setData({
          "moneyObj": res.data
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})